<template>
  <div>
    <div class="gva-search-box">
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline" @keyup.enter="onSubmit">
      <el-form-item label="创建时间">
      <el-date-picker v-model="searchInfo.startCreatedAt" type="datetime" placeholder="开始时间"></el-date-picker>
       —
      <el-date-picker v-model="searchInfo.endCreatedAt" type="datetime" placeholder="结束时间"></el-date-picker>
      </el-form-item>
        <el-form-item label="ATA章节（一级）">
         <el-input v-model="searchInfo.ata_chapter_level1" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="ATA章节（二级）">
         <el-input v-model="searchInfo.ata_chapter_level2" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="故障编号">
         <el-input v-model="searchInfo.fault_number" placeholder="搜索条件" />

        </el-form-item>
        <!-- <el-form-item label="故障名称">
         <el-input v-model="searchInfo.fault_name" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="AO号">
         <el-input v-model="searchInfo.ao_number" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="架次">
         <el-input v-model="searchInfo.sorties" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="排故时间">
            
            <el-date-picker v-model="searchInfo.startTroubleshooting_time" type="datetime" placeholder="搜索条件（起）"></el-date-picker>
            —
            <el-date-picker v-model="searchInfo.endTroubleshooting_time" type="datetime" placeholder="搜索条件（止）"></el-date-picker>

        </el-form-item>
        <el-form-item label="图纸号">
         <el-input v-model="searchInfo.drawing_number" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="登记人">
         <el-input v-model="searchInfo.register_person" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="设计要求的操作和响应">
         <el-input v-model="searchInfo.operation_and_response_to_design_requirements" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="故障现象">
         <el-input v-model="searchInfo.symptoms" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="故障类型">
         <el-input v-model="searchInfo.fault_type" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="结论">
         <el-input v-model="searchInfo.conclusion" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="参考资料">
         <el-input v-model="searchInfo.references" placeholder="搜索条件" />

        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="search" @click="onSubmit">查询</el-button>
          <el-button icon="refresh" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="gva-table-box">
        <div class="gva-btn-list">
            <el-button type="primary" icon="plus" @click="AddForm">新增</el-button>
            <el-popover v-model:visible="deleteVisible" placement="top" width="160">
            <p>确定要删除吗？</p>
            <div style="text-align: right; margin-top: 8px;">
                <el-button type="primary" link @click="deleteVisible = false">取消</el-button>
                <el-button type="primary" @click="onDelete">确定</el-button>
            </div>
            <template #reference>
                <el-button icon="delete" style="margin-left: 10px;" :disabled="!multipleSelection.length" @click="deleteVisible = true">删除</el-button>
            </template>
            </el-popover>
        </div>
        <el-table
        ref="multipleTable"
        style="width: 100%"
        tooltip-effect="dark"
        :data="tableData"
        row-key="ID"
        @selection-change="handleSelectionChange"
        @sort-change="sortChange"
        >
        <el-table-column type="selection" width="55" />
        <el-table-column align="left" label="日期" width="180">
            <template #default="scope">{{ formatDate(scope.row.CreatedAt) }}</template>
        </el-table-column>
        <el-table-column align="left" label="ATA章节（一级）" prop="ata_chapter_level1" width="150" />
        <el-table-column align="left" label="ATA章节（二级）" prop="ata_chapter_level2" width="150" />
        <el-table-column align="left" label="故障编号" prop="fault_number" width="150" />
        <!-- <el-table-column align="left" label="故障名称" prop="fault_name" width="120" />
        <el-table-column align="left" label="AO号" prop="ao_number" width="120" />
        <el-table-column align="left" label="架次" prop="sorties" width="120" />
         <el-table-column sortable align="left" label="排故时间" width="180">
            <template #default="scope">{{ formatDate(scope.row.troubleshooting_time) }}</template>
         </el-table-column>
        <el-table-column align="left" label="图纸号" prop="drawing_number" width="120" />
        <el-table-column align="left" label="登记人" prop="register_person" width="120" />
        <el-table-column align="left" label="登记人工号" prop="register_person_number" width="120" />
        <el-table-column align="left" label="设计要求的操作和响应" prop="operation_and_response_to_design_requirements" width="120" />
        <el-table-column align="left" label="故障现象" prop="symptoms" width="120" />
        <el-table-column align="left" label="排故思路" prop="troubleshooting_ideas" width="120" />
        <el-table-column align="left" label="故障类型" prop="fault_type" width="120" />
        <el-table-column align="left" label="排故步骤" prop="troubleshooting_steps" width="120" />
        <el-table-column align="left" label="故障定位" prop="fault_location" width="120" />
        <el-table-column align="left" label="结论" prop="conclusion" width="120" />
        <el-table-column align="left" label="参考资料" prop="references" width="120" />
        <el-table-column align="left" label="附件上传" prop="upload_attachment" width="120" />
        <el-table-column align="left" label="故障树上传" prop="upload_fault_tree" width="120" /> -->
        <el-table-column align="left" label="操作">
            <template #default="scope">
            <el-button type="primary" link icon="edit" class="table-button" @click="updateFault_file_structFunc(scope.row)">变更</el-button>
            <el-button type="primary" link icon="delete" @click="deleteRow(scope.row)">删除</el-button>
            <el-button type="primary" link icon="delete" @click="create_caseLibrary_form(scope.row)">新建案例录入表</el-button>
            </template>
        </el-table-column>
        </el-table>
        <div class="gva-pagination">
            <el-pagination
            layout="total, sizes, prev, pager, next, jumper"
            :current-page="page"
            :page-size="pageSize"
            :page-sizes="[10, 30, 50, 100]"
            :total="total"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
            />
        </div>
    </div>

  </div>
</template>

<script>
export default {
  name: 'Fault_file_struct'
}
</script>

<script setup>
import {
  createFault_file_struct,
  deleteFault_file_struct,
  deleteFault_file_structByIds,
  updateFault_file_struct,
  findFault_file_struct,
  getFault_file_structList
} from '@/api/sysFaultFile'

// 全量引入格式化工具 请按需保留
import { getDictFunc, formatDate, formatBoolean, filterDict } from '@/utils/format'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()

// 自动化生成的字典（可能为空）以及字段
const formData = ref({
        ata_chapter_level1: '',
        ata_chapter_level2: '',
        fault_number: '',
        fault_name: '',
        ao_number: '',
        sorties: '',
        troubleshooting_time: new Date(),
        drawing_number: '',
        register_person: '',
        register_person_number: '',
        operation_and_response_to_design_requirements: '',
        symptoms: '',
        troubleshooting_ideas: '',
        fault_type: '',
        troubleshooting_steps: '',
        fault_location: '',
        conclusion: '',
        references: '',
        upload_attachment: '',
        upload_fault_tree: '',
        })

// 验证规则
const rule = reactive({
               ata_chapter_level1 : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               ata_chapter_level2 : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               fault_number : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()


// =========== 表格控制部分 ===========
const page = ref(1)
const total = ref(0)
const pageSize = ref(10)
const tableData = ref([])
const searchInfo = ref({})
// 排序
const sortChange = ({ prop, order }) => {
  searchInfo.value.sort = prop
  searchInfo.value.order = order
  getTableData()
}

// 重置
const onReset = () => {
  searchInfo.value = {}
  getTableData()
}

// 搜索
const onSubmit = () => {
  page.value = 1
  pageSize.value = 10
  getTableData()
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

// 修改页面容量
const handleCurrentChange = (val) => {
  page.value = val
  getTableData()
}

// 查询
const getTableData = async() => {
  const table = await getFault_file_structList({ page: page.value, pageSize: pageSize.value, ...searchInfo.value })
  if (table.code === 0) {
    tableData.value = table.data.list
    total.value = table.data.total
    page.value = table.data.page
    pageSize.value = table.data.pageSize
  }
}

getTableData()

// ============== 表格控制部分结束 ===============

// 获取需要的字典 可能为空 按需保留
const setOptions = async () =>{
}

// 获取需要的字典 可能为空 按需保留
setOptions()


// 多选数据
const multipleSelection = ref([])
// 多选
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}

// 删除行
const deleteRow = (row) => {
    ElMessageBox.confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
            deleteFault_file_structFunc(row)
        })
    }

const create_caseLibrary_form = async(row) => {
  router.push({
      name:"case_formNew",
      query:{
        ata_chapter_level1:row.ata_chapter_level1,
        ata_chapter_level2:row.ata_chapter_level2,
        fault_number:row.fault_number
        
      }
      // Newformdata:{
      //   ata_chapter_level1:row.ata_chapter_level1,
      //   ata_chapter_level1:row.ata_chapter_level2,
      //   fault_number:row.fault_number
      // }
    })
  }

// 批量删除控制标记
const deleteVisible = ref(false)

// 多选删除
const onDelete = async() => {
      const ids = []
      if (multipleSelection.value.length === 0) {
        ElMessage({
          type: 'warning',
          message: '请选择要删除的数据'
        })
        return
      }
      multipleSelection.value &&
        multipleSelection.value.map(item => {
          ids.push(item.ID)
        })
      const res = await deleteFault_file_structByIds({ ids })
      if (res.code === 0) {
        ElMessage({
          type: 'success',
          message: '删除成功'
        })
        if (tableData.value.length === ids.length && page.value > 1) {
          page.value--
        }
        deleteVisible.value = false
        getTableData()
      }
    }

// 行为控制标记（弹窗内部需要增还是改）
const type = ref('')

// 更新行
const updateFault_file_structFunc = async(row) => {
  router.push({
      name:"fault_file_form",
      query:{
        id:row.ID
      }
    })
    // const res = await findFault_file_struct({ ID: row.ID })
    // type.value = 'update'
    // if (res.code === 0) {
    //     formData.value = res.data.refault_file_struct
    //     dialogFormVisible.value = true
    // }
}


// 删除行
const deleteFault_file_structFunc = async (row) => {
    const res = await deleteFault_file_struct({ ID: row.ID })
    if (res.code === 0) {
        ElMessage({
                type: 'success',
                message: '删除成功'
            })
            if (tableData.value.length === 1 && page.value > 1) {
            page.value--
        }
        getTableData()
    }
}

// 打开弹窗
const AddForm = () => {
    router.push({
      name:"fault_file_form"
    })
    // type.value = 'create'
    // dialogFormVisible.value = true
}


</script>

<style lang="scss">
  .el-input-number.full-width-input,
  .el-cascader.full-width-input {
    width: 100% !important;
  }
  
  .el-form-item--medium {
    .el-radio {
      line-height: 36px !important;
    }
    
    .el-rate {
      margin-top: 8px;
    }
  }
  
  .el-form-item--small {
    .el-radio {
      line-height: 32px !important;
    }
    
    .el-rate {
      margin-top: 6px;
    }
  }
  
  .el-form-item--mini {
    .el-radio {
      line-height: 28px !important;
    }
    
    .el-rate {
      margin-top: 4px;
    }
  }
  
  .clear-fix:before,
  .clear-fix:after {
    display: table;
    content: "";
  }
  
  .clear-fix:after {
    clear: both;
  }
  
  .float-right {
    float: right;
  }
  
</style>

<style lang="scss" scoped>
  div.table-container {
    table.table-layout {
      width: 100%;
      table-layout: fixed;
      border-collapse: collapse;

      td.table-cell {
        display: table-cell;
        height: 36px;
        border: 1px solid #e1e2e3;
      }
    }
  }
  
  div.tab-container {}
  
  .label-left-align ::v-deep .el-form-item__label {
    text-align: left;
  }
  
  .label-center-align ::v-deep .el-form-item__label {
    text-align: center;
  }
  
  .label-right-align ::v-deep .el-form-item__label {
    text-align: right;
  }
  
  .custom-label {}
  
  .static-content-item {
    min-height: 20px;
    display: flex;
    align-items: center;

    ::v-deep .el-divider--horizontal {
      margin: 0;
    }
  }
  
</style>